package com.coc.controller;

import com.coc.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptController {

    @Autowired
    private RestTemplate restTemplate;

    //要远程连接的地址
//    private String REST_URL_PREFIX="http://localhost:8001";
    //使用ribbon 不在去直接访问ip地址，通过服务名称来获取服务
    private String REST_URL_PREFIX="http://SPRINGCLOUD-PROVIDER-DEPT";

    @GetMapping("/queryDepts")
    public Object queryDepts(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/queryDepts", List.class);
    }

    @GetMapping("/add")
    public Integer add(Dept dept){
        System.out.println(dept);
        return restTemplate.postForObject(REST_URL_PREFIX+"/add",dept,Integer.class);
    }

    @GetMapping("/delete")
    public Integer delete(Integer deptNo){
        return restTemplate.getForObject(REST_URL_PREFIX+"/delete?deptNo="+deptNo,Integer.class);
    }

}
